package com.tec.modules.order.service.impl;

import com.tec.modules.order.domain.OrderDetail;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import com.tec.modules.order.repository.OrderDetailRepository;
import com.tec.modules.order.service.OrderDetailService;
import com.tec.modules.order.service.dto.OrderDetailDto;
import com.tec.modules.order.service.dto.OrderDetailQueryCriteria;
import com.tec.modules.order.service.mapper.OrderDetailMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @author tec
* @date 2020-05-27
*/
@Service
//@CacheConfig(cacheNames = "orderDetail")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class OrderDetailServiceImpl implements OrderDetailService {

    private final OrderDetailRepository orderDetailRepository;

    private final OrderDetailMapper orderDetailMapper;

    public OrderDetailServiceImpl(OrderDetailRepository orderDetailRepository, OrderDetailMapper orderDetailMapper) {
        this.orderDetailRepository = orderDetailRepository;
        this.orderDetailMapper = orderDetailMapper;
    }

    @Override
    //@Cacheable
    public Map<String,Object> queryAll(OrderDetailQueryCriteria criteria, Pageable pageable){
        Page<OrderDetail> page = orderDetailRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(orderDetailMapper::toDto));
    }

    @Override
    //@Cacheable
    public List<OrderDetailDto> queryAll(OrderDetailQueryCriteria criteria){
        return orderDetailMapper.toDto(orderDetailRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    //@Cacheable(key = "#p0")
    public OrderDetailDto findById(Long id) {
        OrderDetail orderDetail = orderDetailRepository.findById(id).orElseGet(OrderDetail::new);
        ValidationUtil.isNull(orderDetail.getId(),"OrderDetail","id",id);
        return orderDetailMapper.toDto(orderDetail);
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public OrderDetailDto create(OrderDetail resources) {
        return orderDetailMapper.toDto(orderDetailRepository.save(resources));
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public void update(OrderDetail resources) {
        OrderDetail orderDetail = orderDetailRepository.findById(resources.getId()).orElseGet(OrderDetail::new);
        ValidationUtil.isNull( orderDetail.getId(),"OrderDetail","id",resources.getId());
        orderDetail.copy(resources);
        orderDetailRepository.save(orderDetail);
    }

    @Override
    //@CacheEvict(allEntries = true)
    public void deleteAll(Long[] ids) {
        for (Long id : ids) {
            orderDetailRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<OrderDetailDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (OrderDetailDto orderDetail : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("订单主表ID", orderDetail.getMasterId());
            map.put("型号", orderDetail.getModel());
            map.put("颜色", orderDetail.getColor());
            map.put("尺码", orderDetail.getSize());
            map.put("男女款", orderDetail.getStyle());
            map.put("件数", orderDetail.getQuantity());
            map.put("单价", orderDetail.getUnitPrice());
            map.put("总价", orderDetail.getTotalPrice());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}
